package org.test.client.ui.component.combox;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.test.client.ui.util.Icons;

public class MyComboxModel {
	//如果 不使用 分组 则将所有 条目都加入 这个 key 的list 里面
	public final static String NONE_GROUP = "$NONEGROUP$";
	
	private Map<String ,List> group = null;
	private Map<String ,List> filterGroup = null;
	private boolean needGroup = false;
	private String filterStr = null;
	public Object selectedObj = null;
	
	public MyCombox combox = null;
	
	public MyComboxModel(Map<String ,List> group){
		this.group = Collections.synchronizedMap(group);
		needGroup = true;
	}
	
	public MyComboxModel(List<Object> list){
		group = new HashMap<String ,List>();
		group.put(NONE_GROUP, list);
		this.group = Collections.synchronizedMap(group);
		needGroup = false;
	}
	
	public MyComboxModel(boolean needGroup){
		group = new HashMap<String ,List>();
		this.group = Collections.synchronizedMap(group);
		this.needGroup = needGroup;
	}
	
	public Map<String ,List> setFilterString(String filterStr) {
		if(filterStr==null || filterStr.length()==0){
			return group;
		}
		if(this.filterStr == filterStr){
			return filterGroup;
		}
		
		this.filterStr = filterStr;
		if(filterGroup == null){
			filterGroup = new HashMap<String, List>();
			filterGroup = Collections.synchronizedMap(filterGroup);
		}else{
			filterGroup.clear();
		}
		
		Set<Entry<String,List>> entrySet = group.entrySet();
		for(Entry<String,List>  tmpEntry: entrySet){
			
			List<Object> tmpList = new ArrayList<Object>();
			String key = tmpEntry.getKey();
			List value = tmpEntry.getValue();
			
			for(Object obj : value){
				String str = obj.toString();
				String lowerCaseStr = str.toLowerCase();
				String lowerCaseFilterStr = filterStr.toLowerCase();
				if(lowerCaseStr.contains(lowerCaseFilterStr)){
					tmpList.add(obj);
				}
			}
			if(tmpList.size() > 0){
				filterGroup.put(key, tmpList);
			}
		}
		
		return filterGroup;
	}

	public boolean getIsGroup() {
		return needGroup;
	}
	
	public void addItem(Object objItem,boolean selected){
		addItemByGroup(NONE_GROUP,objItem ,selected);
	}
	
	public void addItemByGroup(String groupKey,Object objItem,boolean selected){
		if(NONE_GROUP.equals(groupKey) && needGroup){
			return;
		}
		List list = group.get(groupKey);
		if (list == null) {
			list = new ArrayList();
			list.add(objItem);
			group.put(groupKey, list);
		} else {
			list.add(objItem);
		}
		if(selected ){
			selectedObj = objItem;
		}
		//重新添加 条目后 需要重绘 中间 条目面板
		if(combox != null){
			if(combox.ccc != null){
				combox.ccc.doCenterInit();
			}
		}
	}
	

	public Object getSelectedObj() {
		return selectedObj;
	}

	public void setSelectedObj(Object selectedObj) {
		this.selectedObj = selectedObj;
	}

	
	public void clear(){
		
		if(group != null){
			group.clear();
		}
		if(filterGroup != null){
			filterGroup.clear();
		}
		filterStr = null;
		selectedObj = null;
		
	}
	
}

